package com.controller;

import com.entity.Student;
import com.service.StudentServiceImpl;
import spark.Request;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.HelloWorld.gson;
import static spark.Spark.*;

public class StudentController {
    public StudentController() {
    }

    public StudentServiceImpl studentService;

    public void init() throws SQLException {
        studentService = new StudentServiceImpl();

        initGetList();
        initAddStudent();
        initEditStudent();
        initDeleteStudent();
    }

    private void initGetList() {
        get("/student/list", (req, res) -> {
            String user = req.session().attribute("user");

            Map<String, Object> result = new HashMap();

            if (user != null) {
                System.out.println("已登录: " + user);
                result.put("user", user);
            } else {
                System.out.println("未登录");
            }

            List<Student> list;
            try {
                list = studentService.getAllStudents();
            } catch (SQLException e) {
                e.printStackTrace();

                list = new ArrayList<>();
                result.put("result", "fail");
                result.put("data", list);
                return result;
            }

            result.put("result", "success");
            result.put("data", list);
            return result;
        }, gson::toJson);
    }

    private void initAddStudent() {

        post("/student/add", (req, res) -> {

            Student student = getStudentFromReq(req);
            System.out.println("测试  id: " + student.getId() + " name: " + student.getName());

            Map<String, String> result = new HashMap();

            try {
                studentService.addStudent(student);
            } catch (Exception e) {

                e.printStackTrace();

                result.put("result", "fail");
                return result;

            }

            result.put("result", "success");
            return result;

        }, gson::toJson);

    }

    private void initEditStudent() {
        post("/student/update", (req, res) -> {

            Student student = getStudentFromReq(req);
            //System.out.println("测试  id: " + id + " name: " + name);

            Map<String, String> result = new HashMap();

            try {
                studentService.updateStudent(student);
            } catch (Exception e) {

                e.printStackTrace();

                result.put("result", "fail");
                return result;

            }

            result.put("result", "success");
            return result;

        }, gson::toJson);
    }

    private void initDeleteStudent() {
        post("/student/delete", (req, res) -> {

            String id = req.queryParams("id");


            //System.out.println("测试  id: " + id + " name: " + name);

            Map<String, String> result = new HashMap();


            try {
                studentService.deleteStudent(id);
            } catch (Exception e) {

                e.printStackTrace();

                result.put("result", "fail");
                return result;

            }

            result.put("result", "success");
            return result;

        }, gson::toJson);
    }

    private Student getStudentFromReq(Request req) {
        String id = req.queryParams("id");
        String name = req.queryParams("name");
        String sex = req.queryParams("sex");
        String birthday = req.queryParams("birthday");//直接使用字符串格式好了
        String politicalstatus = req.queryParams("politicalstatus");//随便怎么输入都可以
        String address = req.queryParams("address");//随便怎么输入都可以
        String dormnum = req.queryParams("dormnum");//随便怎么输入都可以
        String phone = req.queryParams("phone");//随便怎么输入都可以

        return new Student(id, name, sex, birthday, politicalstatus, address, phone, dormnum);
    }
}
